<template>
  <div class="assets">
    <div>
      <el-button type="primary" size="mini" @click="save">{{ $t('btn.save') }}</el-button>
    </div>
    <el-tabs>
      <el-tab-pane :label="$t('market.info')">
        <el-form :model="proValue" label-width="100px" size="mini">
          <el-form-item :label="$t('market.billType')">
            <el-select v-model="proValue.fbilltypeid">
              <el-option
                v-for="item in billtype1"
                :key="item.fid"
                :label="item.fname"
                :value="item.fid"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('market.businessDate')">
            <el-date-picker
              v-model.trim="proValue.fdate"
              type="date"
              value-format="yyyy-MM-dd"
              style="width: 163px"
            />
          </el-form-item>
          <el-form-item :label="$t('market.disposalWay')">
            <el-select v-model="proValue.fmodeid">
              <el-option
                v-for="item in billtype"
                :key="item.fid"
                :label="item.fname"
                :value="item.fid"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('market.disposalReason')">
            <el-input v-model="proValue.freason" />
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <el-tabs v-model="activeName">
      <el-tab-pane :label="$t('market.assets')" name="first">
        <div style="margin-bottom: 5px">
          <el-button size="mini" type="primary" @click="newHang">{{ $t('btn.newHang') }}</el-button>
          <el-button size="mini" type="danger" @click="delHang">{{ $t('btn.delHang') }}</el-button>
        </div>
        <jc-table
          serial
          :table-data="tableData"
          :table-header="tableHeader"
          table-height="300px"
          @rowClick="rowClick"
        >
          <!--卡片编码-->
          <template v-slot:tagSlot="col">
            <el-input v-model="col.scope.row.fassetsbillno" size="mini">
              <i
                slot="suffix"
                class="el-input__icon el-icon-search"
                @click="handelWorkshop(col.scope.$index)"
              />
            </el-input>
          </template>
          <!--处置数量-->
          <template v-slot:billSlot="col">
            <el-input-number v-model="col.scope.row.fmanageamount" size="mini" />
          </template>
          <!--清理费用-->
          <template v-slot:btnState="col">
            <el-input-number v-model="col.scope.row.fprice" size="mini" />
          </template>
          <!--残值收入-->
          <template v-slot:btnStates="col">
            <el-input-number v-model="col.scope.row.finprice" size="mini" />
          </template>
        </jc-table>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.fince')" name="second">
        <div>
          <el-form :model="proValue1" label-width="100px" size="mini" disabled>
            <el-form-item :label="$t('market.cardNumber')">
              <el-input v-model="proValue1.fassetsbillno" />
            </el-form-item>
            <el-form-item :label="$t('market.assetsname')">
              <el-input v-model="proValue1.fassetsname" />
            </el-form-item>
          </el-form>
        </div>
        <div>
          <jc-table
            :table-data="tableData2"
            :table-header="tableHeader2"
            table-height="300px"
          />
        </div>
      </el-tab-pane>
    </el-tabs>
    <el-tabs>
      <el-tab-pane :label="$t('market.entity')">
        <div style="margin-bottom: 5px">
          <el-button size="mini" type="danger" @click="delHang1">{{ $t('btn.delHang') }}</el-button>
        </div>
        <jc-table
          :table-data="tableData[indexSelf].list"
          :table-header="tableHeader3"
          table-height="300px"
          @rowClick="rowClick1"
        >
          <template v-slot:tagSlot="col">
            <el-input-number v-model="col.scope.row.famount" :max="col.scope.row.fassetsamount" size="mini" />
          </template>
        </jc-table>
      </el-tab-pane>
    </el-tabs>
    <!--  卡片编码  -->
    <assets v-if="assetsVis" @assets="assetsData" />
  </div>
</template>

<script>
import {
  createAssetsManageBill, queryAssetsDetails
} from '@/api/financial/assets'
import { queryViewItem } from '@/api/marketManage/marketOrder'
import { toMxBmina } from '@/components/ToMxamineState'
export default {
  components: {
    assets: () => import('@/views/financial/assets/dispose/component/assets')
  },
  data() {
    return {
      assetsVis: false,
      activeName: 'first',
      proValue: {
        fbilltypeid: 1464,
        fmodeid: 1465,
        freason: '',
        fdate: new Date().toLocaleDateString().split('/').join('-')
      },
      indexSelf: 0,
      billtype: [], // 处置
      billtype1: [], // 处置
      tableData: [
        {
          fassetsbillno: '',
          fassetsname: '',
          fassetsamount: 0,
          funitname: '',
          fmanageamount: 0,
          fcurrencyname: '人民币',
          fcurrencyid: 1,
          fprice: '',
          finprice: '',
          list: []
        }
      ],
      tableHeader: [
        { label: this.$t('market.cardNumber'), type: 'tag', prop: 'fassetsbillno', requireg: 'must' },
        { label: this.$t('market.assetsname'), prop: 'fassetsname' },
        { label: this.$t('market.fqty'), prop: 'fassetsamount' },
        { label: this.$t('market.funit'), prop: 'funitname' },
        { label: this.$t('market.manageamount'), type: 'bill', prop: 'fmanageamount', width: '90', requireg: 'must' },
        { label: this.$t('market.managecurrency'), prop: 'fcurrencyname' },
        { label: this.$t('market.cleaningCost'), type: 'state', prop: 'fprice', width: '90', requireg: 'must' },
        { label: this.$t('market.salvage'), type: 'states', prop: 'finprice', width: '90', requireg: 'must' }
      ],
      matter: [], // 其他信息
      proValue1: {},
      tableData2: [],
      tableHeader2: [
        { label: this.$t('market.policy'), prop: 'fpolicyname' },
        { label: this.$t('market.currency2'), prop: 'fcurrencyname' },
        { label: this.$t('market.assetsoriginal'), prop: 'fassetsoriginalprice' },
        { label: this.$t('market.cumulativedep'), prop: 'fcumulativedepprice' },
        { label: this.$t('market.expectresidualf'), prop: 'fexpectresidualfprice' }
      ],
      // 实物
      tableData3: [],
      tableHeader3: [
        { label: this.$t('market.assetsname'), prop: 'fassetsname' },
        { label: this.$t('market.barcode1'), prop: 'fbarcode' },
        { label: this.$t('market.specify'), prop: 'fmodel' },
        { label: this.$t('market.areaname'), prop: 'fareaname' },
        { label: this.$t('market.assetQty'), prop: 'fassetsamount' },
        { label: this.$t('market.manageamount'), prop: 'famount', type: 'tag' },
        { label: this.$t('market.assetsnum'), prop: 'fassetsbillno' },
        { label: this.$t('market.supplier'), prop: 'fsuppliername' }
      ]
    }
  },
  created() {
    this.queryTBasBilltype()
    this.queryTBasBilltype1()
  },
  methods: {
    // 保存
    save() {
      const DATA = {
        ...this.proValue,
        details: this.tableData
      }
      createAssetsManageBill(DATA).then(res => {
        if (res.code === 0) {
          this.$message({ showClose: true, message: res.message, type: 'success' })
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: `/disposeDetail/${res.data.fid}` })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    }, // 打开卡片编码
    handelWorkshop(index) {
      this.indexSelf = index
      this.assetsVis = true
    }, // 卡片编码
    assetsData(ev) {
      if (ev.fassetsamount <= 0) {
        this.assetsVis = false
        this.$message({ showClose: true, message: '资产数量必须大于0', type: 'warning' })
        return
      }
      const item = this.tableData[this.indexSelf]
      if (ev.fassetsbillno) {
        item.fassetsbillno = ev.fbillno
        item.fassetsname = ev.fassetsname
        item.fassetsamount = ev.fassetsamount
        item.funitname = ev.funitname
        item.fbillid = ev.fid
        item.fentryid = 0
        this.handleGetUntreated(item)
      }
      this.assetsVis = false
    },
    // 详情
    handleGetUntreated(ev) {
      const fid = ev.fbillid
      queryAssetsDetails({ fid }).then(res => {
        if (res.code === 0) {
          this.proValue1 = toMxBmina(res.data)
          this.proValue1.fassetsbillno = this.proValue1.fbillno
          const { fpolicyname, fcurrencyname, fassetsoriginalprice,
            fcumulativedepprice, fexpectresidualfprice, fid } = this.proValue1
          this.tableData2 = [{
            fpolicyname, fcurrencyname, fassetsoriginalprice,
            fcumulativedepprice, fexpectresidualfprice, fid
          }]
          // this.tableData3 = this.proValue1.details
          this.proValue1.details.map(res => {
            res.fassetsname = this.proValue1.fassetsname
            res.fmanageentryid = res.fentryid
            res.fassetsamount = res.famount
          })
          // if (this.tableData[this.indexSelf].list.length === 0) {
          this.tableData[this.indexSelf].list = this.proValue1.details
          // }
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 处置方式
    async queryTBasBilltype() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_MANAGE_MODE' })
      this.billtype = RES.list
    },
    // 资产处置
    async queryTBasBilltype1() {
      const { data: RES } = await queryViewItem({ viewName: 'SX_ASSETS_MANAGE' })
      this.billtype1 = RES.list
    },
    newHang() {
      this.tableData.push({
        fassetsbillno: '',
        fassetsname: '',
        fassetsamount: 0,
        funitname: '',
        fmanageamount: 0,
        fcurrencyname: '人民币',
        fcurrencyid: 1,
        fprice: '',
        finprice: '',
        list: []
      })
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
      if (row.fbillid) {
        this.handleGetUntreated(row)
      }
    },
    // 删除行
    delHang() {
      this.tableData.splice(this.indexSelf, 1)
    },
    // 实物点击行
    rowClick1(row) {
      this.indexSelf1 = row.index
    },
    // 实物删除行
    delHang1() {
      this.tableData[this.indexSelf].list.splice(this.indexSelf1, 1)
    }
  }
}
</script>

<style scoped lang="scss">
.assets {
  @include listBom;
  .title {
    font-size: 14px;
    margin-top: 5px;
    color: #1e9fff;
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      height: 35px;
      width: 275px;
      margin-bottom: 5px;
    }
  }
  .el-table{
    &::v-deep .el-input-number .el-input__inner{
      padding: 0 5px;
      width: 80px;
    }
    &::v-deep .el-input-number .el-input{
      width: 80px;
    }
    &::v-deep .el-input-number--mini{
      width: 80px;
    }
    &::v-deep .el-input-number__increase{
      display: none;
    }
    &::v-deep .el-input-number__decrease{
      display: none;
    }
    &::v-deep .el-table__footer-wrapper tbody td{
      background: #fdf6ec!important;
    }
  }
}
</style>

